CentOS 7
Sponsored Link

Elastic Stack 5 : Install Elasticsearch
2017/04/12
 
Install Full-Text search engine "Elasticsearch".
[1]
[2] Install and Run Elasticsearch.
[root@dlp ~]#
vi /etc/yum.repos.d/elasticsearch.repo
# create new

[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
[root@dlp ~]#
yum -y install elasticsearch
[root@dlp ~]#
systemctl start elasticsearch

[root@dlp ~]#
systemctl enable elasticsearch
# verify working

[root@dlp ~]#
curl http://127.0.0.1:9200

{
  "name" : "QekSksw",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "21ZfbH_cQRud1xwZPZtkYA",
  "version" : {
    "number" : "5.3.0",
    "build_hash" : "3adb13b",
    "build_date" : "2017-03-23T03:31:50.652Z",
    "build_snapshot" : false,
    "lucene_version" : "6.4.1"
  },
  "tagline" : "You Know, for Search"
}
[3] This is the basic usage of Elasticsearch.
Create an Index first, it is like Database on RDB.
# show Index list ([pretty] means it shows JSON with human readable)

[root@dlp ~]#
curl http://127.0.0.1:9200/_aliases?pretty

{ }
# create Index (ElasticSearch 5.0 later, impossible to use POST for creating Index)

[root@dlp ~]#
curl -X PUT "http://127.0.0.1:9200/test_index"

{"acknowledged":true,"shards_acknowledged":true}
# verify

[root@dlp ~]#
curl http://127.0.0.1:9200/_aliases?pretty

{
  "test_index" : {
    "aliases" : { }
  }
}
[root@dlp ~]#
curl http://127.0.0.1:9200/test_index/_settings?pretty

{
  "test_index" : {
    "settings" : {
      "index" : {
        "creation_date" : "1492062413921",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "8h--wu58TiiMVPLcuFYL5Q",
        "version" : {
          "created" : "5030099"
        },
        "provided_name" : "test_index"
      }
    }
  }
}
[5] Define Mapping and insert test data.
Mapping defines structure of Index. If inserting data, Mapping will be defined automatically, but it's possible to define manually, of course.
# insert data

[root@dlp ~]#
curl -X PUT "http://127.0.0.1:9200/test_index/doc01/1" -d '{
    "subject" : "Test Post No.1",
    "description" : "This is the initial post",
    "content" : "This is the test message for using Elasticsearch."
}'

# show Mapping

[root@dlp ~]#
curl "http://127.0.0.1:9200/test_index/_mapping/doc01?pretty"

{
  "test_index" : {
    "mappings" : {
      "doc01" : {
        "properties" : {
          "content" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "description" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "subject" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

# show data

[root@dlp ~]#
curl "http://127.0.0.1:9200/test_index/doc01/1?pretty"

{
  "_index" : "test_index",
  "_type" : "doc01",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "subject" : "Test Post No.1",
    "description" : "This is the initial post",
    "content" : "This is the test message for using Elasticsearch."
  }
}

# search data

# example of Search conditions below means [description] field includes a word [initial]

[root@dlp ~]#
curl "http://127.0.0.1:9200/test_index/doc01/_search?q=description:initial&pretty=true"

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2824934,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "doc01",
        "_id" : "1",
        "_score" : 0.2824934,
        "_source" : {
          "subject" : "Test Post No.1",
          "description" : "This is the initial post",
          "content" : "This is the test message for using Elasticsearch."
        }
      }
    ]
  }
}
 
Tweet